import tfct.ply.lex as lex
import tfct.ply.yacc as yacc
from tfct.fuse import model

class FuseParser:
    tokens = (
        'TAG','WORD',        
        'NUMBER','LINK',                
    )
    
    literals = ['=','+','-','*','/','(',')',':','.','[',']']
    
    # Tokens    
    t_TAG = r'[a-zA-Z][a-zA-Z0-9]*\:'
    t_WORD = r'[a-zA-Z][a-zA-Z0-9]*'    
    t_LINK = r'\[[a-zA-Z][a-zA-Z]*\]'    
    t_ignore = " \t"    

    def __init__(self, conf):
        self.lexer = lex.lex(module=self)
        self.yacc = yacc.yacc(module=self)
        self.manager = model.FUSE({'screenspec': {}})
        self.names = list()
        self.tags = list()
        self.numbers = list()
        self.temp = ""
        self.link = None            
        self.ucname = ""
        self.uclevel = ""
        self.ucsentence = ""
        self.usecase = None
        self.extension = None
        self.conf = conf

    def run_file(self, filename):
        f = open(filename, 'r')
        return self.run_text(f.read())
        
    def run_text(self, text):        
        self.yacc.parse(text)
        
        print self.ucname
        print self.uclevel
        for x in self.names:
            print x
            
        return None # obiekt use case

    def t_NUMBER(self, t):
        r'\d+[a-z0-9]*'
        #t.value = int(t[0].value)
        return t    
    
    def t_newline(self, t):
        r'\n+'
        t.lexer.lineno += t.value.count("\n")
        
    def t_error(self, t):
        print("Illegal character '%s'" % t.value[0])
        t.lexer.skip(1)
    
    
    def p_fuse(self, p):
        '''fuse : uc
                | fuse uc'''    
    
    def p_uc(self, p):
        "uc : title level main_scenario extensions"
        
    def p_title(self, p):
        "title : tag name"
        for x in self.names:
            self.ucname = self.ucname + x + ' '                        
        self.names = []
        
    def p_name(self, p):
        '''name : word
                | name word'''            
            
    def p_level(self, p):
        '''level : tag word''' 
        for x in self.names:
            self.uclevel = self.uclevel + x + ' '                        
        self.names = [] 
        
        self.usecase = self.manager.add_usecase(self.ucname, self.uclevel)      
        
    def p_main_scenario(self, p):
        "main_scenario : mscenario scenarios"
        #for x in self.names:
        #    print x
        
    def p_mscenario(self, p):
        '''mscenario : word
                     | mscenario tag'''
    
    def p_scenarios(self, p):
        '''scenarios : scenario
                    | scenarios scenario'''
        if self.names[0] == "Main":
            self.names.remove("Main")
        
        self.ucsentence = ""
        
        for x in self.names:
            self.ucsentence = self.ucsentence + x + ' '
        self.names = []            
        
        if self.link != None:                        
            self.usecase.add_step(self.ucsentence, self.link)
        else:
            self.usecase.add_step(self.ucsentence)
        self.link = None
        
    def p_scenario(self, p):
        "scenario : number '.' sentence"
        
        
    def p_sentence(self, p):
        '''sentence : word
                    | sentence link
                    | sentence word'''
        
    def p_extensions(self, p):
        '''extensions : tag extension
                      | '''        
    
    def p_extension(self, p):
        '''extension : step
                     | extension step'''
        
    def p_step(self, p):
        "step : number '.' sentence"             
    
    def p_word(self, p):
        'word : WORD'
        if p[1] != None:
            self.names.append(p[1])
            
    def p_tag(self, p):
        'tag : TAG'
        if p[1] != None:
            self.tags.append(p[1])   
            
    def p_link(self, p):
        'link : LINK'
        if p[1] != None:
            self.link = p[1]
            
    def p_number(self, p):
        'number : NUMBER'
        if p[1] != None:
            self.numbers.append(p[1])
    
    def p_error(self, p):
        if p:
            print("Syntax error at '%s'" % p.value)
        else:
            print("Syntax error at EOF")